<template>
	<div v-if="isAuth(['ROOT', 'DEPT:SELECT'])">
		<el-form :inline="true" :model="dataForm" :rules="dataRule" ref="dataForm">
			<el-form-item prop="deptName"><el-input v-model="dataForm.deptName" placeholder="部门名称" size="medium" class="input" clearable="clearable" /></el-form-item>
			<el-form-item>
				<el-button size="medium" type="primary" @click="searchHandle()">查询</el-button>
				<el-button size="medium" type="primary" :disabled="!isAuth(['ROOT', 'DEPT:INSERT'])" @click="addHandle()">新增</el-button>
				<el-button size="medium" type="danger" :disabled="!isAuth(['ROOT', 'DEPT:DELETE'])" @click="deleteHandle()">批量删除</el-button>
			</el-form-item>
		</el-form>
		<el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle" cell-style="padding: 4px 0" size="medium" style="width: 100%;">
			<el-table-column type="selection" :selectable="selectable" header-align="center" align="center" width="50" />
			<el-table-column type="index" header-align="center" align="center" width="100" label="序号">
				<template #default="scope">
					<span>{{ (page - 1) * rows + scope.$index + 1 }}</span>
				</template>
			</el-table-column>
			<el-table-column prop="name" header-align="center" align="center" label="部门名称" min-width="170" />
			<el-table-column prop="realName" header-align="center" align="center" label="部门管理员" min-width="170" />
			<el-table-column prop="parentName" header-align="center" align="center" label="上级部门" min-width="170" />
			<el-table-column prop="tenantId" header-align="center" align="center" label="部门机构" min-width="170" />
			<el-table-column prop="number" header-align="center" align="center" label="员工数量" min-width="140" />
<!--			<el-table-column header-align="number" align="center" label="员工数量" min-width="140">-->
<!--				<template #default="scope">-->
<!--					<span>{{ scope.row.emps }}人</span>-->
<!--				</template>-->
<!--			</el-table-column>-->
      <el-table-column prop="state" header-align="center" align="center" min-width="130" label="部门状态">
        <template #default="scope" >
          <el-tag v-if="scope.row.state === 0" type="success">正常</el-tag>
          <el-tag v-if="scope.row.state === 1" type="info">禁用</el-tag>
        </template>
      </el-table-column>
			<el-table-column prop="desc" header-align="center" align="center" label="备注" min-width="400" />
			<el-table-column header-align="center" align="center" width="150" label="操作">
				<template #default="scope">
					<el-button type="text" size="medium" :disabled="!isAuth(['ROOT', 'DEPT:UPDATE'])" @click="updateHandle(scope.row.id)">修改</el-button>
					<el-button type="text" size="medium" :disabled="!isAuth(['ROOT', 'DEPT:DELETE']) || scope.row.emps > 0" @click="deleteHandle(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination
			@size-change="sizeChangeHandle"
			@current-change="currentChangeHandle"
			:current-page="page"
			:page-sizes="[10, 20, 50]"
			:page-size="rows"
			:total="totalCount"
			layout="total, sizes, prev, pager, next, jumper"
		></el-pagination>
		<add-or-update ref="addOrUpdate" @refreshDataList="loadDataList"></add-or-update>
	</div>
</template>

<script>
import AddOrUpdate from './dept-add-or-update.vue';
export default {
	components: {
		AddOrUpdate
	},
	data: function() {
		return {
			dataForm: {
				deptName: null
			},
			dataList: [],
			page: 1,
			rows: 10,
			totalCount: 0,
			dataListLoading: false,
			dataListSelections: [],
			dataRule: {
				deptName: [{ required: false, pattern: '^[a-zA-Z0-9\u4e00-\u9fa5]{1,10}$', message: '部门名称格式错误' }]
			}
		};
	},
	methods: {
    // 加载数据列表
		loadDataList: function() {
			let that = this;
			that.dataListLoading = true;
			let data = {
        //条件查询
        keyword: that.dataForm.deptName,
				page: that.page,
        //每页条数
				length: that.rows
			};
			that.post('/admin/manager/department/selectDept',data,function(resp) {
				console.log(resp);
        if (resp.data.length != 0){
          that.dataList = resp.data;
          that.totalCount = Number(resp.data.length);
        }
        that.dataListLoading = false;
			});
		},
    //查询部门
		searchHandle: function() {
      //refs访问dataForm表单，validate方法表单验证，valid验证结果回调参数
			this.$refs['dataForm'].validate(valid => {
				if (valid) {
          //clearValidate清楚表单的验证信息，防止二次验证冲突
					this.$refs['dataForm'].clearValidate();
          //就是查询表单为空时，赋值为null
					if (this.dataForm.deptName == '') {
						this.dataForm.deptName = null;
					}
          // 始终跳第一页
					if (this.page != 1) {
						this.page = 1;
					}
          //调用查询列表
					this.loadDataList();
				} else {
					return false;
				}
			});
		},
		selectable: function(row, index) {
			if (row.emps > 0) {
				return false;
			}
			return true;
		},
		selectionChangeHandle: function(val) {
			this.dataListSelections = val;
		},
		sizeChangeHandle: function(val) {
			this.rows = val;
			this.page = 1;
			this.loadDataList();
		},
		currentChangeHandle: function(val) {
			this.page = val;
			this.loadDataList();
		},
		deleteHandle: function(id) {
			let that = this;
			let ids = id
				? [id]
				: that.dataListSelections.map(item => {
						return item.id;
				  });
			if (ids.length == 0) {
				that.$message({
					message: '没有选中记录',
					type: 'warning',
					duration: 1200
				});
			} else {
				that.$confirm(`确定要删除选中的记录？`, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					that.post('admin/manager/department/deleteId',ids,function(resp) {
						if (resp.success) {
							that.$message({
								message: '操作成功',
								type: 'success',
								duration: 1200,
								onClose: () => {
									that.loadDataList();
								}
							});
						} else {
							that.$message({
								message: '未能删除记录',
								type: 'warning',
								duration: 1200
							});
						}
					});
				});
			}
		},
		addHandle: function() {
			this.$nextTick(() => {
				this.$refs.addOrUpdate.init();
			});
		},
		updateHandle: function(id) {
      console.log(id)
			this.$nextTick(() => {
				this.$refs.addOrUpdate.init(id);
			});
		}
	},
	created: function() {
		this.loadDataList();
	}
};
</script>

<style></style>
